package com.etc.sfts.filter;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Servlet Filter implementation class Login
 */
@WebFilter({"/index.jsp","/addMovie.jsp","/addpublic.jsp","/publicmassagelist.jsp","/movieList.jsp","/cinemaList.jsp","/addCinema.jsp"})
public class Login implements Filter {

    /**
     * Default constructor. 
     */
    public Login() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request2=(HttpServletRequest) request;
		HttpSession session=request2.getSession();
		if(session.getAttribute("Manager")==null) {
			response.setContentType("text/html;charset=utf-8");
			PrintWriter pw=response.getWriter();
			pw.print("<script>alert('会话失效，请重新登录！');location.href='login.jsp'</script>");
			pw.flush();
			pw.close();
		}else {
		chain.doFilter(request, response);
		}
		}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
